package jmine.tec.rtm.impl.web.pages;

import jmine.tec.web.pages.HelpTextMessages;
import jmine.tec.web.wicket.pages.form.AbstractFormPage;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import bancosys.tec.exception.MessageCreator;
import bancosys.tec.rtm.impl.exception.domain.ExceptionRecord;
import bancosys.tec.security.annotation.Secure;
import bancosys.tec.security.authorization.UrlPermission;
import bancosys.tec.utils.date.Timestamp;

/**
 * Tela de consulta de detalhe de uma exceção.
 * 
 * @author finx
 * @created Oct 27, 2010
 */
@Secure(id = "URL_SHOW_EXCEPTION_RECORD", permissionType = UrlPermission.class)
public class ConsultaDetalheExceptionRecord extends AbstractFormPage {

    private final ExceptionRecord detalhe;

    private Form<ExceptionRecord> form = null;

    private final Page redirect;

    /**
     * Contrutor.
     * 
     * @param redirect tela que deve ser redirecionada quando clicar no botão "voltar".
     * @param detalhe o {@link ExceptionRecord} tendo seus detalhes exibidos.
     */
    public ConsultaDetalheExceptionRecord(Page redirect, ExceptionRecord detalhe) {
        super();
        this.initForm();
        this.detalhe = detalhe;
        this.redirect = redirect;
        this.createComponents();
    }

    /**
     * Inicializa o formulário da página.
     */
    private void initForm() {
        this.form = new Form<ExceptionRecord>("mainForm");
        this.add(this.form);
    }

    /**
     * Gera os componentes da página.
     */
    private void createComponents() {
        this.form.add(new Label("id", new PropertyModel<String>(this.detalhe, "id")));
        this.form.add(new Label("data", new Model<Timestamp>(new Timestamp(this.detalhe.getData().getTimeInMillis()))));
        this.form.add(new Label("mensagem", new PropertyModel<String>(this.detalhe, "mensagem")));
        this.form.add(new Label("system", new PropertyModel<String>(this.detalhe, "system")));
        this.form.add(new Label("user", new PropertyModel<String>(this.detalhe, "user")));

        TextArea<String> txtStackTrace = new TextArea<String>("stackTrace", new PropertyModel<String>(this.detalhe, "stackFormatada"));

        this.form.add(txtStackTrace);

        this.form.add(new Link<Object>("btnVoltar") {
            @Override
            public void onClick() {
                this.setResponsePage(ConsultaDetalheExceptionRecord.this.redirect);
            }
        });

    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected MessageCreator getHelpTextCreator() {
        return HelpTextMessages.CONSULTA_DETALHE_EXCEPTION_RECORD;
    }
}
